<?php
include_once (_CORE_PATH . "libs/morphy/common.php");
class morphy_class extends phpMorphy implements lib_prototype_class
{
    private $_charsetType = array(
                                    'rus' => 'cp1251',
                                    'eng' => 'cp1250',
                                    'ger' => 'cp1252',
    );

    private $_lang = 'eng';
    
    public function __construct()
    {
        $this->_lang = MozgCfg::GetProp('global//morphy/lang');
        
        switch (MozgCfg::GetProp('global//morphy/storageType'))
        {
            case 'shm':
                $storageType = PHPMORPHY_STORAGE_SHM;     
                break;
            case 'mem':
                $storageType = PHPMORPHY_STORAGE_MEM;     
                break;
            default:   
                $storageType = PHPMORPHY_STORAGE_FILE;                  
        }
        
		// set some options
		$opts = array(
			// storage type, follow types supported
			// PHPMORPHY_STORAGE_FILE - use file operations(fread, fseek) for dictionary access, this is very slow...
			// PHPMORPHY_STORAGE_SHM - load dictionary in shared memory(using shmop php extension), this is preferred mode
			// PHPMORPHY_STORAGE_MEM - load dict to memory each time when phpMorphy intialized, this useful when shmop ext. not activated. Speed same as for PHPMORPHY_STORAGE_SHM type
			'storage' => $storageType,
			// Extend graminfo for getAllFormsWithGramInfo method call
			'with_gramtab' => false,
			// Enable prediction by suffix
			'predict_by_suffix' => true, 
			// Enable prediction by prefix
			'predict_by_db' => true
		);
		
		// Path to directory where dictionaries located
		$dir = _CORE_PATH . "libs/morphy/dicts";
		
		// Create descriptor for dictionary located in $dir directory with russian language
		$dict_bundle = new phpMorphy_FilesBundle($dir, $this->_lang);
		
		// Create phpMorphy instance
		try {
			parent::phpMorphy($dict_bundle, $opts);
		} catch(phpMorphy_Exception $e) {
			die('Error occured while creating phpMorphy instance: ' . $e->getMessage());
		}  
    }

	public function getBaseForm($word)
	{    
	    $word = $this->PrepareInputWord($word);
		return $this->PrepareOutputWord(parent::getBaseForm($word));
	}
	
	public function getAllForms($word) 
	{    
	    $word = $this->PrepareInputWord($word);
		return $this->PrepareOutputWord(parent::getAllForms($word));
	}
	
	public function getPseudoRoot($word) 
	{	
	    $word = $this->PrepareInputWord($word);	
		return $this->PrepareOutputWord(parent::getPseudoRoot($word));
	}
	
	public function getAllFormsWithGramInfo($word) 
	{    
	    $word = $this->PrepareInputWord($word);
		return $this->PrepareOutputWord(parent::getAllFormsWithGramInfo($word));
	} 
	
	public function ConvertToCharset($word)
	{
        return iconv('UTF-8', $this->_charsetType[$this->_lang], $word);	    
	}
	
	public function ConvertFromCharset($word)
	{
        return iconv($this->_charsetType[$this->_lang], 'UTF-8', $word);	    
	}	
	
	private function PrepareInputWord($word)
	{
	    if(!is_array($word)) {
            $word = $this->ConvertToCharset($word);
		} else {
            $word = array_map(array($this, 'ConvertToCharset'), $word);
		}

		return $word;
	}	
	
	public function ConvertArrayItem(&$item, $key)
	{
        $item = $this->ConvertFromCharset($item);	    
	}		
	
	public function PrepareOutputWord($word)
	{
        if (!is_array($word)) return $this->ConvertFromCharset($word);

        array_walk_recursive(&$word, array($this, 'ConvertArrayItem'));
        
        $convertedWordArray = array();
        while (list($k, $v) = each($word))
        {
            $k = is_string($k) ? $this->ConvertFromCharset($k) : $k;
            $convertedWordArray[$k] = is_array($v) ? $v : ($v ? $v : array($k));              	    
        }
        
        return $convertedWordArray;
	}
}